package com.rao.gmall.product.controller;

import com.rao.gmall.common.result.Result;
import com.rao.gmall.product.component.MinioService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.InputStream;

/**
 * @author nzmxfan
 * @create 2021-08-05-10:54
 */
@Slf4j
@RestController
@RequestMapping("/admin/product")
public class FileUploadController {

    @Resource
    MinioService minioService;

    @PostMapping("/fileUpload")
    public Result<String> fileUpload(@RequestPart("file") MultipartFile file){

        try {
            InputStream inputStream = file.getInputStream();
            String fileName = file.getOriginalFilename();
            String upload = minioService.upload(inputStream, null, fileName);
            log.info("upload 的值为：" + upload);
            return Result.ok(upload);
        } catch (Exception e) {
           return Result.fail("网络异常");
        }
    }
}
